<?php

namespace app\common\model;

use think\Model;


class GoodsRebate extends Model
{

    

    

    // 表名
    protected $name = 'goods_rebate';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    
	public function goods() {
		return $this->belongsTo('goods', 'goods_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	/**
	 * 添加编辑商品返利规则
	 * @param $goodsId 商品ID
	 * @param $rebate 返利规则
	 * @return false|int
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit($goodsId, $rebate) {
		$rebate['goods_id'] = $goodsId;
		$row = $this->where(['goods_id' => $goodsId])->find();
		if($row)  {
			$res = $this->allowField(true)
				->save($rebate, ['id' => $row['id']]);
		} else {
			$res = $this->isUpdate(false)->allowField(true)
				->save($rebate);
		}
		
		return $res;
	}




}
